package com.fenghongzhang.day006;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private TabLayout tl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tl = (TabLayout) findViewById(R.id.tl);


        vp = (ViewPager) findViewById(R.id.vp);

//        //数据源
//        List<String> list = new ArrayList<>();
//        list.add("大大大");
//        list.add("123");
//        list.add("123");
//        //适配器
//
//        MyAdapter myAdapter = new MyAdapter(list, this);
//        vp.setAdapter(myAdapter);

        //fragment

        List<Fragment> list = new ArrayList<>();

        list.add(new WelFragment());
        list.add(new TwoFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());
        list.add(new SanFragment());



        List<String> titles = new ArrayList<>();
        titles.add("新闻");
        titles.add("游戏");
        titles.add("足球");
        titles.add("足球");
        titles.add("足球");
        titles.add("足球");
        titles.add("足球");
        titles.add("足球");
        titles.add("足球");

        FGAdapter fgAdapter = new FGAdapter(getSupportFragmentManager(),list,titles);

        vp.setAdapter(fgAdapter);

        //绑定
        tl.setupWithViewPager(vp);


        //设置当前页面
        vp.setCurrentItem(4);

        //监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //是第几页就是几
                if(position == 1){
                    Toast.makeText(MainActivity.this, "呵呵", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
